﻿using System;
using System.Net;

namespace Kurax.BoardGames.Client.Web {
    public partial class Gravatar : System.Web.UI.Page {
        protected void Page_Load(object sender, EventArgs e) {
            var url = string.Format("http://www.gravatar.com/avatar/{0}?s={1}&d=wavatar", this.Request.QueryString["h"], string.IsNullOrEmpty(this.Request.QueryString["s"]) ? "100" : this.Request.QueryString["s"]);

            var buffer = new byte[1024];
            var client = new WebClient();
            using(var stream = client.OpenRead(url)) {
                if(stream == null)
                    return;
                this.Response.ContentType = "image/jpeg";
                var count = stream.Read(buffer, 0, buffer.Length);
                while(count > 0) {
                    this.Response.OutputStream.Write(buffer, 0, count);
                    count = stream.Read(buffer, 0, buffer.Length);
                }
            }
        }
    }
}
